<?php

include_once BONE_ROOT.DS.'controllers'.DS.'vars.php';

class controller_pricing extends controller_theme
{

	public function __construct()
	{
		parent::__construct();
	}
	
	// 最近报价列表
	public function create()
	{
		$model = bone::get_model('pricing');
		$recent_pricings = $model->get_priced_pricings(0, 10);

		$template = bone::get_template('pricing.create');
		$template->set_title('咨询报价');
		$template->set('recent_pricings', $recent_pricings);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function create_save()
	{
		$session = bone::get_session();
		$pricing_captcha = $session->clear('pricing_captcha');

		$captcha = post('captcha','');
		if(strtolower($pricing_captcha) != strtolower($captcha))
		{
			$this->redirect( url('./?controller=pricing&task=create&menu_id='.request('menu_id', 1)), '验证码错误', 'error' );
		}

		$my = bone::get_user();

		$table_pricing = bone::get_table('pricing');
		$table_pricing->bind($_POST);
		$table_pricing->create_time = time();
		$table_pricing->save();

		$names = $_FILES['attachment']['name'];
		$types = $_FILES['attachment']['type'];
		$sizes = $_FILES['attachment']['size'];
		$errors = $_FILES['attachment']['error'];
		$tmp_names = $_FILES['attachment']['tmp_name'];

		$allow_file_types = explode(', ', ALLOW_UPLOAD_FILE_TYLE);
		$allow_image_types = explode(', ', ALLOW_UPLOAD_IMAGE_TYLE);
		
		$t = date('Ymd').'_'.rand(1, 9999);
		for($i=0, $l=count($names); $i<$l; $i++)
		{
			if($errors[$i]==0)
			{
				$type = array_pop(explode('.', $names[$i]));
				if(in_array($type, $allow_file_types))
				{
					$file_name = $t.'_'.$i.'.'.$type;

					$dst_path = BONE_ROOT.DS.'storage'.DS.'project'.DS.'attachment'.DS.$file_name;
					move_uploaded_file($tmp_names[$i], $dst_path);

					$table_attachment = bone::get_table('project_attachment');
					$table_attachment->pricing_id = $table_pricing->id;
					$table_attachment->user_id = $my->id;
					$table_attachment->name = $names[$i];
					$table_attachment->type = $type;
					$table_attachment->size = $sizes[$i];
					$table_attachment->file_name = $file_name;
					if(in_array($type, $allow_image_types))
					{
						$image = bone::get('image');
						$image->open( $dst_path );
						if( $image->is_image() )
						{
							$image->resize_to(400, 300, 1);
							$image->save_to( BONE_ROOT.DS.'storage'.DS.'project'.DS.'image'.DS.$t.'_'.$i.'.'.$image->get_type());
							$table_attachment->image = $t.'_'.$i.'.'.$image->get_type();

							$image->resize_to(40, 30);
							$image->save_to( BONE_ROOT.DS.'storage'.DS.'project'.DS.'thumbnail'.DS.$t.'_'.$i.'.'.$image->get_type());
							$table_attachment->thumbnail = $t.'_'.$i.'.'.$image->get_type();
						}
					}
					$table_attachment->create_time = time();
					$table_attachment->save();

					print_r($table_attachment);
				}
				else
				{
					unlink($tmp_names[$i]);
				}
			}
		}
		$this->redirect( url('./?controller=pricing&task=detail&pricing_id='.$table_pricing->id.'&menu_id='.request('menu_id', 1)), '您的项目已提交，请耐心等待工程师报价。', 'notice' );
	}

	public function detail()
	{
		$pricing_id = get('pricing_id', 0, 'int');
		if(!$pricing_id) exit;

		$table = bone::get_table('pricing');
		$table->load($pricing_id);

		$model = bone::get_model('pricing');
		$attachments = $model->get_attachments($pricing_id);

		$template = bone::get_template('pricing.detail');
		$template->set_title($table->name.' 报价详情');
		$template->set('pricing', $table);

		if($table->manager_id)
		{
			$model = bone::get_model('team');
			$template->set('manager', $model->get_user($table->manager_id));
		}

		$template->set('attachments', $attachments);
		$template->set_data($this->get_data());
		$template->display();
	}


	public function listing()
	{
		$model = bone::get_model('pricing');
		$template = bone::get_template('pricing.listing');
		$template->set_title('历史报价列表');

		$limit = 50;
		$pagination = $template->get_pagination();
		$pagination->set_limit( $limit );
		$pagination->set_total($model->get_priced_pricing_sum());
		$pagination->set_page(get('page', 1, 'int'));
		$pagination->set_url('./?controller=pricing&task=listing&menu_id='.request('menu_id', 1));

		$template->set('priced_pricings', $model->get_priced_pricings($pagination->get_offset(), $limit) );
		$template->set_data($this->get_data());
		$template->display();
	}


}
?>